//
//  M_WenbaDetail.m
//  suRead
//
//  Created by nancy  on 16/9/13.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "M_WenbaDetail.h"
#import "M_Question.h"

@implementation M_WenbaDetail
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"f_id": @"id",
             @"f_title": @"title",
             @"f_square":@"square",
             @"f_views":@"views",
             @"f_updateTime": @"updateTime",
             @"f_background_img": @"background_img",
             @"f_zhuanjia_id": @"zhuanjia_id",
             @"f_zhuanjia_img": @"zhuanjia_img",
             @"f_zhuanjia_name": @"zhuanjia_name",
             @"f_zhuanjia_touxian": @"zhuanjia_touxian",
             @"f_zhuanjia_description": @"zhuanjia_description",
             @"f_zhuanjia_status": @"zhuanjia_status",
             @"f_zhuanjia_proNum": @"zhuanjia_proNum",
             @"f_zhuanjia_replyNum": @"zhuanjia_replyNum",
             @"f_shareurl": @"shareurl",
             @"f_category":@"category",
             @"f_questions":@"questions"
             };
}

+ (NSValueTransformer *)f_questionsJSONTransformer {
   
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSArray *value_, BOOL *success, NSError **error){
        
        if (value_ && [value_ isKindOfClass:[NSArray class]]) {
            NSArray *list = [MTLJSONAdapter modelsOfClass:[M_Question class] fromJSONArray:value_ error:nil];
            
            return list;
        }
        else {
            return @[];
        }
        
    }];
    
}


+ (NSValueTransformer *)f_updateTimeJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSNumber *value_, BOOL *success, NSError **error){
        
        if (!value_) {
            return @" ";
        }
        
        double deltaSeconds = fabs([[NSDate date] timeIntervalSince1970] - value_.doubleValue); // 获取总共秒数
        double deltaMinutes = deltaSeconds / 60.0f;
        
        NSString *dateContent;
        if (deltaSeconds < 5) {
            dateContent = @"刚刚";
        } else if (deltaSeconds < 120) {
            dateContent = @"1分钟前";
        } else if (deltaMinutes < 60) {
            dateContent = [NSString stringWithFormat:@"%i分钟前", (int)deltaMinutes];
        } else if (deltaMinutes < 120) {
            dateContent = @"1小时前";
        } else if (deltaMinutes < (24 * 60)) {
            dateContent = [NSString stringWithFormat:@"%i小时前", (int)floor(deltaMinutes / 60)];
        } else if (deltaMinutes < (24 * 60 * 2)) {
            dateContent = @"昨天";
        } else if (deltaMinutes < (24 * 60 * 7)) {
            dateContent = [NSString stringWithFormat:@"%d天前", (int)floor(deltaMinutes/(60 * 24))];
        } else if (deltaMinutes < (24 * 60 * 14)) {
            dateContent = @"上周";
        } else if (deltaMinutes < (24 * 60 * 31)) {
            dateContent = [NSString stringWithFormat:@"%d周前", (int)floor(deltaMinutes/(60 * 24 * 7))];
        } else if (deltaMinutes < (24 * 60 * 61)) {
            dateContent = @"上个月";
        } else if (deltaMinutes < (24 * 60 * 365.25)) {
            dateContent = [NSString stringWithFormat:@"%d月前", (int)floor(deltaMinutes/(60 * 24 * 30))];
        } else if (deltaMinutes < (24 * 60 * 731)) {
            dateContent = @"去年";
        } else {
            dateContent = [NSString stringWithFormat:@"%i年前", (int)floor(deltaMinutes/(60 * 24 * 731))];
        }
        
        return dateContent;
        
    } reverseBlock:^(NSString *id_, BOOL *success, NSError **error) {
        return id_;
    }];
    
}

@end